package tools

import (
	"bufio"
	"fmt"
	"io"
	"os"
	"strings"
)

type ProtoMessageData struct {
	Name string
}
type ProtoBuffFileData struct {
	PkgName  string
	Messages []*ProtoMessageData
}

func ScanProtoBuffFile(filePath string) *ProtoBuffFileData {
	f, err := os.Open(filePath)
	if err != nil {
		fmt.Println(err)
	}
	retData := &ProtoBuffFileData{}

	retData.Messages = make([]*ProtoMessageData, 0)

	rb := bufio.NewReader(f)
	for {
		line, _, err := rb.ReadLine()

		if err == io.EOF {
			break
		}
		strLine := strings.TrimSpace(string(line))
		worldArr := strings.Split(strLine, " ")
		if worldArr == nil || len(worldArr) <= 0 {
			continue
		}

		if strings.TrimSpace(worldArr[0]) == "message" {
			retData.Messages = append(retData.Messages, &ProtoMessageData{
				Name: strings.TrimSpace(worldArr[1])})
		}
		if strings.TrimSpace(worldArr[0]) == "package" {
			retData.PkgName = strings.TrimRight(strings.TrimSpace(worldArr[1]), ";")
		}
	}
	return retData
}
